package cn.iocoder.yudao.module.train.controller.admin.payrefundorder;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.payrefundorder.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.payrefundorder.PayRefundOrderDO;
import cn.iocoder.yudao.module.train.service.payrefundorder.PayRefundOrderService;

@Tag(name = "管理后台 - 退款订单")
@RestController
@RequestMapping("/train/pay-refund-order")
@Validated
public class PayRefundOrderController {

    @Resource
    private PayRefundOrderService payRefundOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建退款订单")
    @PreAuthorize("@ss.hasPermission('train:pay-refund-order:create')")
    public CommonResult<Long> createPayRefundOrder(@Valid @RequestBody PayRefundOrderSaveReqVO createReqVO) {
        return success(payRefundOrderService.createPayRefundOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新退款订单")
    @PreAuthorize("@ss.hasPermission('train:pay-refund-order:update')")
    public CommonResult<Boolean> updatePayRefundOrder(@Valid @RequestBody PayRefundOrderSaveReqVO updateReqVO) {
        payRefundOrderService.updatePayRefundOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除退款订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:pay-refund-order:delete')")
    public CommonResult<Boolean> deletePayRefundOrder(@RequestParam("id") Long id) {
        payRefundOrderService.deletePayRefundOrder(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除退款订单")
                @PreAuthorize("@ss.hasPermission('train:pay-refund-order:delete')")
    public CommonResult<Boolean> deletePayRefundOrderList(@RequestParam("ids") List<Long> ids) {
        payRefundOrderService.deletePayRefundOrderListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得退款订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:pay-refund-order:query')")
    public CommonResult<PayRefundOrderRespVO> getPayRefundOrder(@RequestParam("id") Long id) {
        PayRefundOrderDO payRefundOrder = payRefundOrderService.getPayRefundOrder(id);
        return success(BeanUtils.toBean(payRefundOrder, PayRefundOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得退款订单分页")
    @PreAuthorize("@ss.hasPermission('train:pay-refund-order:query')")
    public CommonResult<PageResult<PayRefundOrderRespVO>> getPayRefundOrderPage(@Valid PayRefundOrderPageReqVO pageReqVO) {
        PageResult<PayRefundOrderDO> pageResult = payRefundOrderService.getPayRefundOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayRefundOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退款订单 Excel")
    @PreAuthorize("@ss.hasPermission('train:pay-refund-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPayRefundOrderExcel(@Valid PayRefundOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayRefundOrderDO> list = payRefundOrderService.getPayRefundOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "退款订单.xls", "数据", PayRefundOrderRespVO.class,
                        BeanUtils.toBean(list, PayRefundOrderRespVO.class));
    }

}